package com.yhc.test;

import java.io.*;


public class ObjectSer {
	public void play() {
		File file = new File("d:/abc/ww.txt");
		//saveMyObject(file);
		getObject(file);
	}// play
	public void saveMyObject(File file) {
		FileOutputStream fos;
		ObjectOutputStream oos;
		WangWang w1 = new WangWang("tom", 6);
		WangWang w2 = new WangWang("bean", 8);
		try {
			fos = new FileOutputStream(file,true);
			oos = new MyOutputStream(fos);
			oos.writeObject(w1);
			oos.writeObject(w2);
			oos.flush();
			fos.flush();
			oos.close();
			fos.close();
			System.out.println("存对象啦");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}// save

	public void getObject(File file) {
		FileInputStream fis;
		ObjectInputStream ois;
		try {
			fis = new FileInputStream(file);
			ois = new MyInputStream(fis);
			while (true) {
				try {
					WangWang w = (WangWang) ois.readObject();
					System.out.println(w.getName() +":"+ w.getAge());
				} catch (EOFException e) {
					break;
				}
			}// while
			ois.close();
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}// get
}
class  WangTest{
	public static void main(String[] args) {
	   //new 	ObjectSer().play();
		//save();
		get();
	}
	public static  void save(){
		OutputStream out = null;
		ObjectOutputStream oos = null;
		try {
			out = new FileOutputStream("c:/temp/t.txt",true);
			oos = new MyOutputStream(out);
			String t1="s1";
			String t2="s2";
            oos.writeObject(t1);
			oos.writeObject(t2);
            oos.close();
            out.close();
			System.out.println("存对象ok!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}//save
	//取对象
	public  static  void get(){
		InputStream in = null;
		ObjectInputStream ois = null;
		try {
			in = new FileInputStream("c:/temp/t.txt");
			ois = new MyInputStream(in);
			while (true){
				try {
					String temp=(String) ois.readObject();
					System.out.println(temp);
				}catch (EOFException ex){
					break;
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}